//
//  EmoticonsManager.swift
//  EmoticonKeyBoard
//
//  Created by apple on 15/12/26.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

//负责从plist文件中加载表情数据
class EmoticonsManager: NSObject {
    
    
   
    //分组表情数组
    lazy var packages = [EmoticonPackges]()
    
    //实例化的时候加载分组表情
    override init() {
        super.init()
        loadEmoticon()
    }
    
    
    func loadEmoticon() {
        //1获取bundle 文件目录
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")
       //判断path是否为空
        guard let filePath = path else {
            print("文件目录不存在")
            return
        }
//        print(path)
        
        //2加载plist文件  转换为字典
        let dict = NSDictionary(contentsOfFile: filePath)!
        //3获取字典中的数据 遍历数据 获取分组表情目录中Info.plist
        if let array = dict["packages"] as? [[String : AnyObject]] {
            //遍历数组
            for item in array {
                let id = item["id"] as! String
                //获取分组表情的目录  加载 分组目录中的  Info.plist
                loadGroupEmoticons(id)
            }
        }
    }
    
    private func loadGroupEmoticons(id: String) {
        let path = NSBundle.mainBundle().pathForResource("Info.plist", ofType: nil, inDirectory: "Emoticons.bundle/" + id)
        guard let filePath = path else {
            print("文件目录不存在")
            return
        }
        //一定加载到Info.plist文件
        let dict = NSDictionary(contentsOfFile: filePath)!

        let group_name_cn = dict["group_name_cn"] as! String
        
        let array = dict["emoticons"] as! [[String : String]]

        //实例化packages 对应的模型
        let p = EmoticonPackges(id: id, group_name_cn: group_name_cn, array: array)
        
        packages.append(p)
        
    }
}
